package com.rd.sys.servlet;

import java.io.File;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import baseproj.common.util.LoggerUtil;
import baseproj.common.web.taglib.IsmTag;
import baseproj.inter.I18NHandler;

import com.rd.sys.common.utils.CommonUtils;
import com.rd.sys.common.utils.GetWebConfig;

/**
 * Servlet implementation class InitServlet
 */
public class StartServlet extends HttpServlet {
    /**
     * 
     */
    private static final long serialVersionUID = 8116219822239884792L;

    protected static final Logger logger = LoggerFactory.getLogger(StartServlet.class);

    private String version = "1.01.02";
    private String resourcetype = "zh";
    private String webType = "rd";

    /**
     * 系统初始化
     * 
     * 
     * @return
     * @throws
     */
    @Override
    public void init() throws ServletException {
        try {
            /** 获取门户版本信息 */
            version = GetWebConfig.getValue("version");

            /** 读取国际化资源文件 */
            resourcetype = GetWebConfig.getValue("resourcetype");
            I18NHandler.initial("GlobalResource", webType, resourcetype);

            /** 检查所需目录是否为空 */
            File userProjectBaseDir = new File(CommonUtils.judgeConfigPath(GetWebConfig
                    .getValue("user.project.base.path"))); // 项目分析目录
            if (!userProjectBaseDir.exists()) {
                userProjectBaseDir.mkdirs();
            }
            File syncTempFileDir = new File(CommonUtils.judgeConfigPath(GetWebConfig
                    .getValue("inf.sync.file.temp.path"))); // 同步临时文件目录
            if (!syncTempFileDir.exists()) {
                syncTempFileDir.mkdirs();
            }

            /** 设置JS更新标签 */
            IsmTag.setVersionSwitch(false); // 打开版本开关
            IsmTag.setVersion(false); // 默认都是增加版本标识后缀
            IsmTag.setCurrentVersion(version); // 版本标识

            IsmTag.setDebugSwitch(true); // 打开调试开关
            IsmTag.setDebug(true); // 默认都是调试版,即不加后缀

            /** 将启动的日志记录到tomcat/logs/version.txt中 */
            LoggerUtil.writeStartLog("EyeVision Analyzer start success! Version: " + version + "\n");

            logger.info("the running path is: " + System.getProperty("user.dir"));
            logger.info("\n********* EyeVision Analyzer start successful! *********");
        } catch (Exception ex) {
            LoggerUtil.writeStartLog("EyeVision WEB start failed! Version: " + version + "\n");

            logger.error("\n********* EyeVision Analyzer initialize fail! *********", ex);
            throw new ServletException(ex);
        }
    }

    /**
     * Destroy.
     * 
     * @version
     */
    @Override
    public void destroy() {
    }
}
